package 加密与安全.非对称加密;

import org.apache.commons.codec.binary.Base64;
//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;
//import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Cipher;
//import java.io.IOException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/*
Apache Commons Codec提供了许多编解码相关的工具类。Codec目前最新版本是1.15，最低要求Java7以上。

maven坐标如下：

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>

    //字节数组转Base64编码
    public static String byteToBase64(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }
 
    //Base64编码转字节数组
    public static byte[] base64ToByte(String base64Key) throws IOException {
        return Base64.decodeBase64(base64Key);
    }
    String sha1 = DigestUtils.sha1Hex("测试");//import org.apache.commons.codec.digest.DigestUtils;
    System.out.println(sha1);
*/
public class RSAUtil {
 
    /**
     * 获取RSA公私钥匙对
     */
    private static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048); //512、1024、2048
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return keyPair;
    }
 
    /**
     * 获取公钥(base64编码)
     */
    private static String getPublicKey(KeyPair keyPair) {
        PublicKey publicKey = keyPair.getPublic();
        byte[] bytes = publicKey.getEncoded();
        //return Base64Tool.byteToBase64(bytes);
        return Base64.encodeBase64String(bytes);
    }
 
    /**
     * 获取私钥(Base64编码)
     */
    private static String getPrivateKey(KeyPair keyPair) {
        PrivateKey privateKey = keyPair.getPrivate();
        byte[] bytes = privateKey.getEncoded();
        //return Base64Tool.byteToBase64(bytes);
        return Base64.encodeBase64String(bytes);
    }
 
    public static String[] genKeyPair() throws Exception {
        KeyPair keyPair = getKeyPair();
        String[] keyPairArr = new String[2];
        keyPairArr[0] = getPublicKey(keyPair);
        keyPairArr[1] = getPrivateKey(keyPair);
        return keyPairArr;
    }

    /**
     * 将Base64编码后的公钥转换成PublicKey对象
     */
    public static PublicKey string2PublicKey(String pubStr) throws Exception {
        //byte[] keyBytes = Base64Tool.base64ToByte(pubStr);
        byte[] keyBytes = Base64.decodeBase64(pubStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
    }
 
    /**
     * 将Base64编码后的私钥转换成PrivateKey对象
     */
    public static PrivateKey string2PrivateKey(String priStr) throws Exception {
        byte[] keyBytes = Base64.decodeBase64(priStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }
 
    /**
     * 公钥加密
     */
    public static String publicEncrypt(String content, String publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, RSAUtil.string2PublicKey(publicKey));
        byte[] byteEncrypt = cipher.doFinal(content.getBytes("utf-8"));
        //String msg = Base64Tool.byteToBase64(byteEncrypt);
        String msg = Base64.encodeBase64String(byteEncrypt);
        return msg;
    }
 
    /**
     * 私钥解密
     */
    public static String privateDecrypt(String contentBase64, String privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, RSAUtil.string2PrivateKey(privateKey));
        byte[] bytesDecrypt = cipher.doFinal(Base64.decodeBase64(contentBase64));
        String msg = new String(bytesDecrypt, "utf-8");
        return msg;
    }

}